<?php

    class Partners_model extends CI_Model{

        function __construct()
        {
            parent::__construct();
        }

        /* @params
        * 
        * accounting_code, address, bank_ac_number,bank_name,birthdate,birthplace,citizenship,city,company_person     
        * contract_nr,contract_type,country,default_option_period, due_date,email, language, mobile_phone
        * name, owner_type[], pasport_number, personal_ID,  post_action, post_type
        * sex, surname, swift,   tax_type, telefax, telephone, telephone2, zip_code    
        */

        function create_partner()
        {

            unset($_POST['post_action']);
            unset($_POST['post_type']);

            /*Parse data*/
            $_POST['customer'] = in_array('customer', $_POST['owner_type']) ? TRUE : FALSE;
            $_POST['suplier'] = in_array('suplier', $_POST['owner_type']) ? TRUE : FALSE;
            $_POST['partner'] = in_array('partner', $_POST['owner_type']) ? TRUE : FALSE;            
            unset($_POST['owner_type']);

            $_POST['birthdate'] = strtotime($_POST['birthdate']);


            $this->db->insert('owners', $_POST);

            if($this->db->affected_rows()>0){
                return array('action'=>true,'msg'=>'Successful insert operation.');
            }else{
                return array('action'=>false,'msg'=>'Something went wrong. Please try again.');
            }

        }

        function update_partner()
        {             

            $ID = $_POST['post_ID'];

            unset($_POST['post_ID']);
            unset($_POST['post_action']);
            unset($_POST['post_type']);

            /*Parse data*/
            $_POST['customer'] = in_array('customer', $_POST['owner_type']) ? TRUE : FALSE;
            $_POST['suplier'] = in_array('suplier', $_POST['owner_type']) ? TRUE : FALSE;
            $_POST['partner'] = in_array('partner', $_POST['owner_type']) ? TRUE : FALSE;            
            unset($_POST['owner_type']);

            $_POST['birthdate'] = strtotime($_POST['birthdate']);

            $this->db->where('ID',$ID);
            $this->db->update('owners', $_POST);

            if($this->db->affected_rows()>=0){
                return array('action'=>true,'msg'=>'Successful edit operation.');
            }else{
                return array('action'=>false,'msg'=>'The Owners data is same as before.');
            }

        }

        function delete($post_ID)
        {

            // Delete owner
            $this->db->where('ID',$post_ID);
            $this->db->delete('owners');

            if($this->db->affected_rows()>=0){
                return TRUE;
            }else{
                return TRUE;
            }

        }

        function view_all_partners()
        {

            $res = $this->db->get('partners')->result_array();
            return $res;

        }

        function view_edit_partner($ID)
        {

            $this->db->where('ID',$ID);
            $res = $this->db->get('partners')->row_array();
            return $res;

        }

    }

?>